import functools


class Solution(object):
    def longestCommonSubsequence(self, text1, text2):
        n1 = len(text1)
        n2 = len(text2)

        @functools.cache
        def search(index1, index2):
            if index1 == n1 or index2 == n2:
                return 0
            if text1[index1] == text2[index2]:
                return 1 + search(index1 + 1, index2 + 1)
            else:
                return max(search(index1 + 1, index2), search(index1, index2 + 1))

        return search(0, 0)